﻿@page "/admin/post/recycle-bin"
@using Moonglade.Core.PostFeature
@using Moonglade.Data.Specifications
@inject IMediator Mediator
@{
    ViewBag.Title = "Recycle Bin";
    var posts = await Mediator.Send(new ListPostSegmentByStatusQuery(PostStatus.Deleted));
}

@section scripts{
    <script type="module">
        import * as utils from '/js/app/utils.module.js'

        $('.btn-delete').click(function () {
            var cfm = confirm('Delete Confirmation?');
            if (cfm) {
                deletePost($(this).data('postid'));
            }
        });

        $('.btn-restore').click(function () {
            restorePost($(this).data("postid"));
        });

        $('.btn-empty-recbin').click(function () {
            callApi(`/api/post/recyclebin`, 'DELETE', {},
                (resp) => {
                    blogToast.success('Cleared');
                    setTimeout(function () {
                        window.location.reload();
                    }, 800);
                });
        });

        function deletePost(postid) {
            callApi(`/api/post/${postid}/destroy`, 'DELETE', {},
                (resp) => {
                    document.querySelector(`#post-${postid}`).remove();
                    blogToast.success('Post deleted');
                });
        }

        function restorePost(postid) {
            callApi(`/api/post/${postid}/restore`, 'POST', {},
                (resp) => {
                    document.querySelector(`#post-${postid}`).remove();
                    blogToast.success('Post restored');
                });
        }

        utils.formatUtcTime();
    </script>
}
@Html.AntiForgeryToken()

@section admintoolbar{
    <partial name="_PostManageHeader" />
}

<div class="admin-main-entry-container">
    @if (posts.Any())
    {
        foreach (var post in posts.OrderByDescending(p => p.CreateTimeUtc))
        {
            <div id="post-@post.Id" class="p-3 mb-2 rounded-3 shadow-sm border bg-white">
                <div class="d-flex">
                    <div class="flex-grow-1">
                        <h6>
                            @post.Title
                        </h6>
                        <div>
                            <i class="bi-calendar-event"></i>
                            <time data-utc-label="@post.CreateTimeUtc.ToString("u")">@post.CreateTimeUtc</time>
                        </div>
                    </div>
                    <div>
                        <a class="btn btn-sm btn-outline-accent btn-restore" data-postid="@post.Id" href="javascript:;">
                            <i class="bi-reply-fill"></i> @SharedLocalizer["Restore"]
                        </a>
                        <a class="btn btn-outline-danger btn-sm btn-delete" data-postid="@post.Id" href="javascript:;">
                            <i class="bi-trash"></i> @SharedLocalizer["Delete"]
                        </a>
                    </div>
                </div>
            </div>
        }
    }
    else
    {
        <partial name="_NoPosts" />
    }
</div>

<div class="admin-settings-action-container border-top pt-2 mt-2">
    <a href="#" class="btn btn-outline-danger btn-empty-recbin mb-3">
        <i class="bi-trash"></i>
        @SharedLocalizer["Empty Recycle Bin"]
    </a>
</div>